package com.kaishengit.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.InventoryDao;
import com.kaishengit.dao.SalesBackDao;
import com.kaishengit.dao.SalesRecordsDao;
import com.kaishengit.pojo.Inventory;
import com.kaishengit.pojo.SalesBack;
import com.kaishengit.pojo.SalesRecords;

@Service
@Transactional
public class SalesBackService {
	private SalesBackDao salesBackDao;
	private InventoryDao inventoryDao;
	private SalesRecordsDao salesRecordsDao;
	
	public void add(SalesBack salesBack){
		Inventory inventory = inventoryDao.findByGoodsid(salesBack.getGoods().getId());
		if(inventory == null){
			Inventory inv = new Inventory();
			inv.setNumber(salesBack.getBacknum());
			inv.setGoods(salesBack.getGoods());
			
			inventoryDao.add(inv);
		}else{
			inventory.setNumber(inventory.getNumber() + salesBack.getBacknum());
			inventory.setGoods(salesBack.getGoods());
			inventoryDao.update(inventory);
		}
		SalesRecords salesRecords = salesRecordsDao.findBySalesBack(salesBack);
		salesRecords.setNumber(salesRecords.getNumber() - salesBack.getBacknum());
		salesRecords.setGoods(salesBack.getGoods());
		salesRecords.setCustomer(salesBack.getCustomer());
			
		salesRecordsDao.update(salesRecords);
		
		salesBackDao.add(salesBack);
	}
	
	public List<SalesBack> findAll(){
		return salesBackDao.findAll();
	}
	public List<SalesBack> findByWhere(SalesBack salesBack){
		return salesBackDao.findByWhere(salesBack);
	}
	@Autowired
	public void setSalesBackDao(SalesBackDao salesBackDao) {
		this.salesBackDao = salesBackDao;
	}
	@Autowired
	public void setInventoryDao(InventoryDao inventoryDao) {
		this.inventoryDao = inventoryDao;
	}
	@Autowired
	public void setSalesRecordsDao(SalesRecordsDao salesRecordsDao) {
		this.salesRecordsDao = salesRecordsDao;
	}
	
	
	

}
